<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Commission extends Controllers{

	public function index(){
		$page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$size = 20;
		$limit = ($page - 1)*$size;

		$user = Session::get("user");
		if($user['power'] == 1){
		$sql = "select * from  ims_xuan_zy_commission_team as t left join ims_mc_members as m on t.uid = m.uid  where t.partner_id = {$user['uid']} and m.power in (0,2)";
		$sql1 = "select count(*) as count  from  ims_xuan_zy_commission_team as t left join ims_mc_members as m on t.uid = m.uid  where t.partner_id = {$user['uid']} and m.power in (0,2)";
		} else if($user['power'] == 2) {
		$sql = "select * from  ims_xuan_zy_commission_team as t left join ims_mc_members as m on t.uid = m.uid  where t.team_id = {$user['uid']} and m.power = 0";
		$sql1 = "select count(*) as count from  ims_xuan_zy_commission_team as t left join ims_mc_members as m on t.uid = m.uid  where t.team_id = {$user['uid']} and m.power = 0";
		}
		$sql .=  " limit {$limit} , {$size}";
		$arr = DB::query($sql);
		$count = DB::query($sql1)[0]['count'];
		foreach ($arr as $k => $v) {
			if($v['power'] == 2){
				$arr[$k]['power']  = "团队";
			} else if ($v['power'] == 0){
				$arr[$k]['power']  = "用户";
			}
		}
		$this->assign("list",$arr);
		$this->assign("count",$count);
		if(request()->isajax()){
			echo  json_encode($arr);die;
		}
	  	return $this->fetch("admin/commission");		
	}
	public function model(){
		$user = Session::get("user");
		$sql  = "select * from ims_xuan_zy_commission_model where uid = {$user['uid']} and type = 1";
		$arr = DB::query($sql);
		$this->assign("list",$arr);
		return $this->fetch("admin/com_model");

	}
	public function add(){
		$user = Session::get("user");
		$id = $_REQUEST['id'];
		if($id){
			$sql = "select * from ims_xuan_zy_commission_model where id = {$id}";
			$info = DB::query($sql)[0];
			$info['bank'] = json_decode($info['bank'],true);
			$bank = [];
			foreach ($info['bank']['bank'] as $k => $v) {
				$bank[$k]['bank_name'] = $k;
				$bank[$k]['one'] = $v['one'];
				$bank[$k]['two'] = $v['two'];
				$bank[$k]['three'] = $v['three'];

			}
			$this->assign("id",$id);
			$this->assign("info",$info);
		} else {
			$sql ="select bankname as bank_name from ims_xuan_zy_bank where status = 1";
			$bank = DB::query($sql);
		}
		$this->assign("bank",$bank);
		return $this->fetch("admin/com_add");
	}
	public function bank_upd(){
		$user = Session::get("user");
		$data = $_REQUEST['data'];
		$id  = $_REQUEST['id'];
		$model_name = $_REQUEST['model_name'];
		$sql = "select com_model_id from ims_xuan_zy_commission_team where uid = '{$user['uid']}'";
		$m_id = DB::query($sql)[0]['com_model_id'];
		$sql = "select bank from ims_xuan_zy_commission_model where id = '{$m_id}'";
		$bank = json_decode(DB::query($sql)[0]['bank'],true);
		foreach ($data['bank'] as $k => $v) {
				$sum  = array_sum($v);
				$sql = "select  commision from ims_xuan_zy_commission where  uid  = '{$user['uid']}' and bank_name = '{$k}'";
				$commission = DB::query($sql)[0]['commision'];
				if ($commission) {
					if ($sum>$commission) {
						$this->error("{$k}佣金设置错误应小于{$commission}");
					}
				} else {
					if ($sum>$bank[$k]) {
						$this->error("{$k}佣金设置错误应小于{$bank[$k]}");
					}
				}
		}
		if ($id) {
				DB::table("ims_xuan_zy_commission_model")->where("id",$id)->setField(["model_name"=>$model_name,"bank"=>json_encode($data)]);
				$this->success("修改成功","admin/commission/model");	

		} else {
			DB::table("ims_xuan_zy_commission_model")->insert(["model_name"=>$model_name,"bank"=>json_encode($data),"uid"=>$user['uid'],"type"=>1]);
			$this->success("添加成功","admin/commission/model");	


		}

	}
	public function default(){
		  $user = Session::get("user");
		$id = $_REQUEST['id'];
		if($user['power'] == 0){
			$uid = 0;
		} else {
			$uid = $user['uid'];
		} 
		$sql = "update ims_xuan_zy_commission_model set `default`= 1 where id = {$id} and uid = {$uid} and type = 1";
		DB::query($sql);
		$sql = "update ims_xuan_zy_commission_model set `default` = 0 where id <> {$id} and uid = {$uid} and type = 1 ";
		DB::query($sql);
		$this->success("修改成功","admin/commission/model");
	}
	public function bank(){
		  $user = Session::get("user");
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		if($user['power'] == 0){
			$uid = 0;
		} else {
			$uid = $user['uid'];
		} 
		$sql = "select * from ims_xuan_zy_commission_model where uid = {$uid} and type = 1";
		$model = DB::query($sql);
		$sql = "select yj_model_id from ims_xuan_zy_commission_team where uid = {$id}";
		$model_id = DB::query($sql)[0]["yj_model_id"];
		$this->assign("model",$model);
		$this->assign("model_id",$model_id);
		return $this->fetch("admin/model_bank");

	}
	public function bank_add(){
		$uid =$_REQUEST['id'];
		$model_id = $_REQUEST['model_id'];
		$sql = "update ims_xuan_zy_commission_team set yj_model_id = {$model_id} where uid = {$uid}" ;
		DB::query($sql);
	    $user = Session::get("user");
		
		$this->success("修改成功","admin/commission/index");			
	}
	public function bank_model(){

		$uid = $_REQUEST['id'];
		$sql = "select * from ims_xuan_zy_commission where uid = '{$uid}'";
		$arr = DB::query($sql);
		foreach ($arr as $key => $value) {
		$arr[$key]['info'] = json_decode($arr[$key]['info'],true);
		}
		$this->assign("list",$arr);
		$this->assign("uid",$uid);
		return $this->fetch("admin/bank_model");
	}
	public function add_bank(){
		$uid = $_REQUEST['uid'];
		$id = $_REQUEST['id'];
		if($id){
			$sql = "select * from ims_xuan_zy_commission where id = {$id}";
			$info = DB::query($sql)[0];
			$info['info'] = json_decode($info['info'],true);
			$this->assign("info",$info);
		}else{
			$sql = "select bankname from ims_xuan_zy_bank where status = 1 ";
			$bank = DB::query($sql);
			$this->assign("bank",$bank);
		}
		$this->assign("uid",$uid);
			return $this->fetch("admin/model_bankadd");



	}
	public function model_upd(){
		$user = Session::get("user");
		$sql = "select com_model_id from ims_xuan_zy_commission_team where uid = '{$user['uid']}'";
		$model_id = DB::query($sql)[0]['com_model_id'];
		$sql = "select bank from ims_xuan_zy_commission_model where id = '{$model_id}'";
		$model = json_decode(DB::query($sql)[0]['bank'],true);
		$sql = "select commision from ims_xuan_zy_commission where uid = '{$user['uid']}' and bank_name = '{$_REQUEST['bank_name']}'";
		$commision = DB::query($sql)[0]['commision'];
		if($commision){
			$money = $commision;
		}else {
			$money = $model[$_REQUEST['bank_name']];
		}
		if($money<array_sum($_REQUEST['data'])){
			$this->error("佣金设置错误应小于{$money}");
		}
		$id = $_REQUEST['id'];
		$uid = $_REQUEST['uid'];
		$sql = "select power from ims_mc_members where uid = {$uid}";

		$data = [
			'uid'=>$uid,
			'power'=>DB::query($sql)[0]['power'],
			'bank_name'=>$_REQUEST['bank_name'],
			'type'=>$_REQUEST['type'],
			'info'=>json_encode($_REQUEST['data'])
	
		];
		if($id){
			DB::table("ims_xuan_zy_commission")->where("id",$id)->setField($data);
            $this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/commission/bank_model?id={$uid}");
		} else {
			$sql = "select id from ims_xuan_zy_commission where uid = {$uid} and bank_name = '{$_REQUEST['bank_name']}'";
			$id = DB::query($sql)[0]['id'];
			if($id){
			DB::table("ims_xuan_zy_commission")->where("id",$id)->setField($data);

            $this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/commission/bank_model?id={$uid}");
			}else{
			DB::table("ims_xuan_zy_commission")->insert($data);
            $this->success("添加成功","{$_SERVER['SCRIPT_NAME']}/admin/commission/bank_model?id={$uid}");
			}
		}
	}
}